package com.traffic.api.restful;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.traffic.api.filter.ApiCommonFilter;
import com.traffic.api.filter.ApiCustomeFactoryFilter;
import com.traffic.api.filter.ApiOrderFactoryFilter;
import com.traffic.api.filter.ApiParamsFactoryFilter;
import com.traffic.api.filter.ResponseTOcustomer;

public class CheckRestful {

	@Context
	private HttpServletRequest httpRequest;

	public Map<String, Object> check(JSONObject param, String type) throws JSONException {
		Iterator<?> it = param.keys();
		Map<String, Object> requestParams = new HashMap<String, Object>();
		while (it.hasNext()) {
			String parmKey = (String) it.next();
			requestParams.put(parmKey, param.get(parmKey));
		}
		String ip = getIpAddress(this.httpRequest);
		if ((null != ip) && (!("".equals(ip)))) {
			requestParams.put("ip", ip);
		}
		if ("order".equals(type)) {
			// 参数验证
			ApiParamsFactoryFilter.check(requestParams);
			// 客户基本信息验证
			ApiCustomeFactoryFilter.check(requestParams);
			// 订单验证
			ApiOrderFactoryFilter.check(requestParams);
			// 订单生成
			return ResponseTOcustomer.doResponse();
		} else {
			ApiCustomeFactoryFilter.check(requestParams);
			return requestParams;
		}
	}
	public String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");

		if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("HTTP_CLIENT_IP");
			}
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip)))
				ip = request.getRemoteAddr();
		} else if (ip.length() > 15) {
			String[] ips = ip.split(",");
			for (int index = 0; index < ips.length; ++index) {
				String strIp = ips[index];
				if (!("unknown".equalsIgnoreCase(strIp))) {
					ip = strIp;
					break;
				}
			}
		}
		return ip;
	}
	
	public Map<String, Object> checkParam(JSONObject param,String type) throws JSONException {
		Iterator<?> it = param.keys();
		Map<String, Object> requestParams = new HashMap<String, Object>();
		while (it.hasNext()) {
			String parmKey = (String) it.next();
			requestParams.put(parmKey, param.get(parmKey));
		}
		String ip = getIpAddress(this.httpRequest);
		if ((null != ip) && (!("".equals(ip)))) {
			requestParams.put("ip", ip);
		}
		if ("order".equals(type)) {
			ApiCommonFilter.check(requestParams);
			ApiOrderFactoryFilter.check(requestParams);
			return ResponseTOcustomer.doResponse();
		}else {
			ApiCommonFilter.check(requestParams);
			return requestParams;
		}
		
		
	}
}